package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;

    //根据手机号查询会员
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }
    //新增会员
    public void add(Member member) {
         memberDao.add(member);
    }

    @Override
    public Map<String, Object> getMemberReport() {
        //获取最近一年的月份
        List<String> months = DateUtils.getThisYearMonths();
        //创建存储每月会员人数的集合
        List<Long> memberCount = new ArrayList<>();
        //获取每个月对应的会员总数
        for (String month : months) {
            month += "-01";
            String thisMonthLastDay = DateUtils.parseDate2String(DateUtils.getThisMonthLastDay(month));
            long count = memberDao.getMemberCountByMonth(thisMonthLastDay);
            memberCount.add(count);
        }

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("months" , months);
        resultMap.put("memberCount" , memberCount);
        return resultMap;
    }
    //会员性别占比
    @Override
    public List<Map> getSexReport() {
        return memberDao.getSexReport();
    }

    //年龄段占比
    @Override
    public List<Map> ageReport() {
        List<String> birthday = memberDao.ageReport();
        Map map1 = new HashMap();
        Map map2 = new HashMap();
        Map map3 = new HashMap();
        int a=0;
        int b=0;
        int c=0;
        for(int i=0;i<birthday.size();i++){
            String age = birthday.get(i);
            int ageByBirthDay = getAgeByBirthDay(age);
            if(ageByBirthDay > 0 && ageByBirthDay<=18) {
                map1.put("name","0-18");
                map1.put("value",a++);
            }else if(ageByBirthDay >18 && ageByBirthDay<=60){
                map2.put("name","18-60");
                map2.put("value",b++);
            }else if(ageByBirthDay>60){
                map3.put("name"," 60以上");
                map3.put("value",c++);
            }
        }
        List<Map> maps = new ArrayList<>();
        maps.add(map1);
        maps.add(map2);
        maps.add(map3);
        System.out.println("maps:"+maps.toString());
        return maps;
    }
    public static int getAgeByBirthDay(String birthDay){
        if (birthDay == null || birthDay.length()<4) {
            return 0;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //得到当前的年份
        String cYear = sdf.format(new Date()).substring(0,4);
        String cMouth = sdf.format(new Date()).substring(5,7);
        String cDay = sdf.format(new Date()).substring(8,10);
        //得到生日年份
        String birth_Year = birthDay .substring(0,4);
        String birth_Mouth = birthDay .substring(5,7);
        String birth_Day = birthDay .substring(8,10);
        //年月日比较后得到年龄
        int age = Integer.parseInt(cYear) - Integer.parseInt(birth_Year);
        if ((Integer.parseInt(cMouth) - Integer.parseInt(birth_Mouth))<0) {
            age=age-1;
        }else if ((Integer.parseInt(cMouth) - Integer.parseInt(birth_Mouth))==0) {
            if ( (Integer.parseInt(cDay) - Integer.parseInt(birth_Day))>0) {
                age=age-1;
            }else {
                age = Integer.parseInt(cYear) - Integer.parseInt(birth_Year);
            }
        }else if ((Integer.parseInt(cMouth) - Integer.parseInt(birth_Mouth))>0) {
            age = Integer.parseInt(cYear) - Integer.parseInt(birth_Year);
        }
        return age;
    }
}
